home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9173 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  74 lines

  1. Path: library.erc.clarkson.edu!rpi!not-for-mail
  2. From: Shalom Reich <sqr1874@acf4.nyu.edu>
  3. Newsgroups: comp.lang.c++,comp.lang.c++.moderated
  4. Subject: Re: Argc & Argv
  5. Date: 28 Feb 1996 15:00:58 -0000
  6. Organization: Bankers Trust Company
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: Dietmar.Kuehl@uni-konstanz.de
  9. Message-ID: <4h1qna$8is@netlab.cs.rpi.edu>
  10. References: <4gta9f$df5@netlab.cs.rpi.edu>
  11. NNTP-Posting-Host: netlab.cs.rpi.edu
  12. X-Original-Date: Tue, 27 Feb 1996 13:35:07 -0500
  13.  
  14. Rick Richert wrote:
  15. >
  16. > I have some global objects defined outside the main routine and I would
  17. > like to pass the argc and argv values to their constructors.  For example,
  18. > SomeClass obj( argc, argv);
  19. > int main( int argc, char **argv) {
  20. >    blah, blah, blah
  21. > }
  22. > Unfortunately, the compiler tells me that argc and argv are not defined
  23. > or else not available for obj.
  24. > Currently, I get around this problem by creating the global obj and then
  25. > inside of main, I initialize obj via a method that takes argc and argv.
  26. >
  27. > I would prefer to pass the arg vars to the constructor.  Does anyone know
  28. > how I can pass the arg vars without being inside of main?
  29.  
  30. The obj object is defined outside of main which means the constructor will 
  31. be called before main is started.  In this case argc and argv don't exist
  32. when the obj constructor is called and your compiler is correct.
  33.  
  34. Since you don't have the information needed to initialize the object, the 
  35. question becomes whether you really want to create the object at this point
  36. or whether you need a global handle to the object to be available (whenever
  37. it gets created).
  38.  
  39. If a global handle is enough, you might want to change your code as follows:
  40.  
  41.   
  42.  SomeClass * obj;
  43.  
  44.  int main( int argc, char **argv) {
  45.  
  46.     obj = new SomeClass( argc, argv);
  47.  
  48.     blah, blah, blah
  49.  
  50.  }
  51.  
  52. The obj object will now be created where you were initializing the 
  53. members.
  54.  
  55. Since obj is now a *pointer* instead of an object all references to 
  56. obj's members/functions need to be changed from obj.xxx to obj->xxx.
  57.  
  58. I hope this helps.
  59.  
  60. Shalom Reich
  61.  
  62.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  63.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  64.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  65.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  66.